function [Answer,  update]=inputdlg_offscreen(Prompt, Title, NumLines, DefAns,Resize)
%  bcoe@biomed.queensu.ca touched up the following...
%  function [Answer,  update]=inputdlg(Prompt, Title, NumLines, DefAns,Resize)
%  INPUTDLG Input dialog box.
%  Answer = INPUTDLG(Prompt) creates a modal dialog box that returns
%  user input for multiple prompts in the cell array Answer.  Prompt
%  is a cell array containing the Prompt strings.
%
%  INPUTDLG uses WAITFOR to suspend execution until the user responds.
%
%  Answer = INPUTDLG(Prompt,Title) specifies the Title for the dialog.
%
%  Answer = INPUTDLG(Prompt,Title,LineNo) specifies the number of lines
%  for each answer in LineNo.  LineNo may be a constant value or a
%  column vector having one element per Prompt that specifies how many
%  lines per input.  LineNo may also be a matrix where the first
%  column specifies how many rows for the input field and the second
%  column specifies how many columns wide the input field should be.
%
%  Answer = INPUTDLG(Prompt,Title,LineNo,DefAns) specifies the default
%  answer to display for each Prompt.  DefAns must contain the same
%  number of elements as Prompt and must be a cell array.
%
%  Answer = INPUTDLG(Prompt,Title,LineNo,DefAns,AddOpts) specifies whether
%  the dialog may be resized or not.  Acceptable values for AddOpts are
%  'on' or 'off'.  If the dialog can be resized, then the dialog is
%  not modal.
%
%  AddOpts may also be a data structure with fields Resize,
%  WindowStyle and Interpreter.  Resize may be 'on' or 'off'.
%  WindowStyle may be 'modal' or 'normal' and Interpreter may be
%  'tex' or 'none'.  The interpreter applies to the prompt strings.
%
%  Examples:
%
%  prompt={'Enter the matrix size for x^2:','Enter the colormap name:'};
%  def={'20','hsv'};
%  dlgTitle='Input for Peaks function';
%  lineNo=1;
%  answer=inputdlg(prompt,dlgTitle,lineNo,def);
%
%  AddOpts.Resize='on';
%  AddOpts.WindowStyle='normal';
%  AddOpts.Interpreter='tex';
%  answer=inputdlg(prompt,dlgTitle,lineNo,def,AddOpts);
%
%  See also TEXTWRAP, QUESTDLG, WAITFOR.

%  Loren Dean   May 24, 1995.
%  Copyright 1998-2002 The MathWorks, Inc.
%  $Revision: 1.58 $

%%%%%%%%%%%%%%%%%%%%%
%%% General Info. %%%
%%%%%%%%%%%%%%%%%%%%%
Black      =[0       0        0      ]/255;
LightGray  =[192     192      192    ]/255;
LightGray2 =[160     160      164    ]/255;
MediumGray =[128     128      128    ]/255;
White      =[255     255      255    ]/255;

%%%%%%%%%%%%%%%%%%%%
%%% Nargin Check %%%
%%%%%%%%%%%%%%%%%%%%
if nargin == 1 & nargout == 0,
    if strcmp(Prompt,'InputDlgResizeCB'),
        LocalResizeFcn(gcbf)
        return
    end
end

error(nargchk(1,5,nargin));
%error(nargoutchk(1,1,nargout));
error(nargoutchk(1,2,nargout)); %bcoe

if nargin==1,
    Title=' ';
end

if nargin<=2, NumLines=1;end

if ~iscell(Prompt),
    Prompt={Prompt};
end

NumQuest=numel(Prompt);
if NumQuest>8 % double up
    doubleup=true;
    rows=[ceil(NumQuest/2),NumQuest-ceil(NumQuest/2)];
else
    doubleup=false;
end
if nargin<=3,
    DefAns=cell(NumQuest,1);
    for lp=1:NumQuest, DefAns{lp}=''; end
end

WindowStyle='modal';
Interpreter='none';
if nargin<=4,
    Resize = 'off';
end

if nargin==5 & isstruct(Resize),
    Interpreter=Resize.Interpreter;
    WindowStyle=Resize.WindowStyle;
    Resize=Resize.Resize;
end

if strcmp(Resize,'on'),
    WindowStyle='normal';
end

% Backwards Compatibility
if isstr(NumLines),
    warning(['Please see the INPUTDLG help for correct input syntax.' 10 ...
        '         OKCallback no longer supported.' ]);
    NumLines=1;
end

[rw,cl]=size(NumLines);
OneVect = ones(NumQuest,1);
if (rw == 1 & cl == 2)
    NumLines=NumLines(OneVect,:);
elseif (rw == 1 & cl == 1)
    NumLines=NumLines(OneVect);
elseif (rw == 1 & cl == NumQuest)
    NumLines = NumLines';
elseif rw ~= NumQuest | cl > 2,
    error('NumLines size is incorrect.')
end

if ~iscell(DefAns),
    error('Default Answer must be a cell array in INPUTDLG.');
end

%%%%%%%%%%%%%%%%%%%%%%%
%%% Create InputFig %%%
%%%%%%%%%%%%%%%%%%%%%%%
FigWidth=300;FigHeight=100;
if doubleup
    FigWidth=FigWidth*2;
end
FigPos(3:4)=[FigWidth FigHeight];
FigColor=get(0,'Defaultuicontrolbackgroundcolor');
TextForeground = Black;
if sum(abs(TextForeground - FigColor)) < 1
    TextForeground = White;
end
%InputFig=figure(2);
%set(2,'Units'           ,'points' )

InputFig=dialog(...%dialog
    'Visible'         ,'off'      , ...
    'Name'            ,Title      , ...
    'Pointer'         ,'arrow'    , ...
    'Units'           ,'points'   , ...
    'UserData'        ,''         , ...
    'Tag'             ,Title      , ...
    'HandleVisibility','on'       , ...
    'Color'           ,FigColor   , ...
    'NextPlot'        ,'add'      , ...    
    'Resize'          ,Resize       ...
    );%'WindowStyle'     ,WindowStyle, ...


%%%%%%%%%%%%%%%%%%%%%
%%% Set Positions %%%
%%%%%%%%%%%%%%%%%%%%%
DefOffset=5;
SmallOffset=2;

DefBtnWidth=50;
BtnHeight=20;
BtnYOffset=DefOffset;
BtnFontSize=get(0,'FactoryUIControlFontSize');
BtnWidth=DefBtnWidth;

TextInfo.Units              ='points'   ;
TextInfo.FontSize           =BtnFontSize;
TextInfo.HorizontalAlignment='left'     ;
TextInfo.HandleVisibility   ='callback' ;

StInfo=TextInfo;
StInfo.Style              ='text'     ;
StInfo.BackgroundColor    =FigColor;
StInfo.ForegroundColor    =TextForeground ;

TextInfo.VerticalAlignment='bottom';

EdInfo=StInfo;
EdInfo.Style='edit';
EdInfo.BackgroundColor=White;

BtnInfo=StInfo;
BtnInfo.Style='pushbutton';
BtnInfo.HorizontalAlignment='center';

% Determine # of lines for all Prompts

ExtControl=uicontrol(StInfo, ...
    'String'   ,''         , ...
    'Position' ,[DefOffset                  DefOffset  ...
    0.96*(FigWidth-2*DefOffset) BtnHeight  ...
    ]            , ...
    'Visible'  ,'off',         ...
    'units', 'points' ...
    );
%set(ExtControl,'visible','on')%%%%%%%%%%%%%%%%%%%
WrapQuest=cell(NumQuest,1);
QuestPos=zeros(NumQuest,4);

if doubleup
    set(ExtControl,'Position',[DefOffset                  DefOffset  ...
        0.96*(FigWidth/2-2*DefOffset) BtnHeight]);
    for ExtLp=1:rows(1),
        if size(NumLines,2)==2
            [WrapQuest{ExtLp},QuestPos(ExtLp,1:4)]= ...
                textwrap(ExtControl,Prompt(ExtLp),NumLines(ExtLp,2));
        else
            [WrapQuest{ExtLp},QuestPos(ExtLp,1:4)]= ...
                textwrap(ExtControl,Prompt(ExtLp),80);
        end
    end
    set(ExtControl,'Position',[FigWidth/2+2*DefOffset DefOffset                ...
        0.96*(FigWidth/2-2*DefOffset) BtnHeight]);
    
    for ExtLp=rows(1)+1:NumQuest
        if size(NumLines,2)==2
            [WrapQuest{ExtLp},QuestPos(ExtLp,1:4)]= ...
                textwrap(ExtControl,Prompt(ExtLp),NumLines(ExtLp,2));
        else
            [WrapQuest{ExtLp},QuestPos(ExtLp,1:4)]= ...
                textwrap(ExtControl,Prompt(ExtLp),80);
        end
    end % for ExtLp
else
    for ExtLp=1:NumQuest,
        if size(NumLines,2)==2
            [WrapQuest{ExtLp},QuestPos(ExtLp,1:4)]= ...
                textwrap(ExtControl,Prompt(ExtLp),NumLines(ExtLp,2));
        else
            [WrapQuest{ExtLp},QuestPos(ExtLp,1:4)]= ...
                textwrap(ExtControl,Prompt(ExtLp),80);
        end
    end % for ExtLp
end

delete(ExtControl);
QuestHeight=QuestPos(:,4);

TxtHeight=QuestHeight(1)/size(WrapQuest{1,1},1);

EditHeight=TxtHeight*NumLines(:,1);
EditHeight(NumLines(:,1)==1)=EditHeight(NumLines(:,1)==1)+4;
TxtXOffset=QuestPos(:,1);
if doubleup
    FigHeight=(rows(1)+2)*DefOffset    + ...
        BtnHeight+EditHeight(1)*rows(1) + ...
        QuestHeight(1)*rows(1);
    TxtWidth=(FigWidth/2)-2*DefOffset;
    QuestYOffset=zeros(NumQuest,1);
    EditYOffset=zeros(NumQuest,1);
    QuestYOffset(1)=FigHeight-DefOffset-QuestHeight(1);
    EditYOffset(1)=QuestYOffset(1)-EditHeight(1);% -SmallOffset;
    for YOffLp=2:rows(1),
        QuestYOffset(YOffLp)=EditYOffset(YOffLp-1)-QuestHeight(YOffLp)-DefOffset;
        EditYOffset(YOffLp)=QuestYOffset(YOffLp)-EditHeight(YOffLp); %-SmallOffset;
    end % for YOffLp
    for YOffLp=rows(1)+1:NumQuest,
        QuestYOffset(YOffLp)=EditYOffset(YOffLp-rows(1))-QuestHeight(YOffLp)-DefOffset;
        EditYOffset(YOffLp)=QuestYOffset(YOffLp)-EditHeight(YOffLp); %-SmallOffset;
    end % for YOffLp
    
else
    %TxtXOffset=DefOffset;
    FigHeight=(NumQuest+2)*DefOffset    + ...
        BtnHeight+sum(EditHeight) + ...
        sum(QuestHeight);
    TxtWidth=FigWidth-2*DefOffset;
    QuestYOffset=zeros(NumQuest,1);
    EditYOffset=zeros(NumQuest,1);
    QuestYOffset(1)=FigHeight-DefOffset-QuestHeight(1);
    EditYOffset(1)=QuestYOffset(1)-EditHeight(1);% -SmallOffset;
    for YOffLp=2:NumQuest,
        QuestYOffset(YOffLp)=EditYOffset(YOffLp-1)-QuestHeight(YOffLp)-DefOffset;
        EditYOffset(YOffLp)=QuestYOffset(YOffLp)-EditHeight(YOffLp); %-SmallOffset;
    end % for YOffLp
end % double up

QuestHandle=[];
EditHandle=[];
FigWidth =1;

AxesHandle=axes('Parent',InputFig,'Position',[0 0 1 1],'Visible','off');
%%set(AxesHandle,'visible','on')
for lp=1:NumQuest,
    QuestTag=['Prompt' num2str(lp)];
    EditTag=['Edit' num2str(lp)];
    if ~ischar(DefAns{lp}),
        delete(InputFig);
        error('Default answers must be strings in INPUTDLG.');
    end
    QuestHandle(lp)=text('Parent',AxesHandle, ...
        TextInfo     , ...
        'Position'   ,[ TxtXOffset(lp) QuestYOffset(lp) ], ...
        'String'     ,WrapQuest{lp}                 , ...
        'Color'      ,TextForeground                , ...
        'Interpreter',Interpreter                   , ...
        'Tag'        ,QuestTag                        ...
        );
    
    EditHandle(lp)=uicontrol(InputFig   ,EdInfo     , ...
        'Max'       ,NumLines(lp,1)       , ...
        'Position'  ,[ TxtXOffset(lp) EditYOffset(lp) ...
        TxtWidth   EditHeight(lp)  ...
        ]                    , ...
        'String'    ,DefAns{lp}           , ...
        'Tag'       ,QuestTag               ...
        );
    if size(NumLines,2) == 2,
        set(EditHandle(lp),'String',char(ones(1,NumLines(lp,2))*'x'));
        Extent = get(EditHandle(lp),'Extent');
        NewPos = [TxtXOffset EditYOffset(lp)  Extent(3) EditHeight(lp) ];
        
        NewPos1= [TxtXOffset QuestYOffset(lp)];
        set(EditHandle(lp),'Position',NewPos,'String',DefAns{lp})
        set(QuestHandle(lp),'Position',NewPos1)
        
        FigWidth=max(FigWidth,Extent(3)+2*DefOffset);
    else
        FigWidth=max(175,TxtWidth+2*DefOffset);
    end
    
end % for lp
if doubleup
    FigWidth=TxtWidth*2+5*DefOffset;
end
FigWidth=max(FigWidth,2*(BtnWidth+DefOffset)+DefOffset);


Monitorpixels=get(0,'MonitorPositions');
mainwindow=getWidth(com.mathworks.mde.desk.MLDesktop.getInstance.getMainFrame)/2 ...
    +getX(com.mathworks.mde.desk.MLDesktop.getInstance.getMainFrame.location);% added by bcoe

FigPos=get(InputFig,'Position');
Temp=get(0,'Units');
set(0,'Units','points');
Monitorpoints=get(0,'MonitorPositions');
set(0,'Units',Temp);

if mainwindow>Monitorpixels(1,1) && mainwindow<Monitorpixels(1,3)
    FigPos(1)=(Monitorpoints(1,1)+((Monitorpoints(1,3)-Monitorpoints(1,1))/2)-(FigWidth/2));% added by bcoe to  screen 1
    FigPos(2)=(Monitorpoints(1,4)-FigHeight)/1.75;
else
    FigPos(1)=(Monitorpoints(end,1)+((Monitorpoints(end,3)-Monitorpoints(end,1))/2)-(FigWidth/2));% added by bcoe to screen 2
    FigPos(2)=(Monitorpoints(end,4)-FigHeight)/1.75;
end
FigPos(3)=FigWidth;
FigPos(4)=FigHeight;
FigPos=round(FigPos);
set(InputFig,'Position',FigPos);


CBString='set(gcbf,''UserData'',''Cancel'');uiresume';

CancelHandle=uicontrol(InputFig   ,              ...
    BtnInfo     , ...
    'Position'  ,[FigWidth-BtnWidth-DefOffset DefOffset ...
    BtnWidth  BtnHeight  ...
    ]           , ...
    'String'    ,'Cancel'    , ...
    'Callback'  ,CBString    , ...
    'Tag'       ,'Cancel'      ...
    );


CBString='set(gcbf,''UserData'',''OK'');uiresume';

OKHandle=uicontrol(InputFig    ,              ...
    BtnInfo     , ...
    'Position'  ,[ FigWidth-2*BtnWidth-2*DefOffset DefOffset ...
    BtnWidth                    BtnHeight ...
    ]           , ...
    'String'     ,'OK'        , ...
    'Callback'   ,CBString    , ...
    'Tag'        ,'OK'          ...
    );
%%%%% bcoe added
CBString='set(gcbf,''UserData'',''Update'');uiresume';
UDHandle=uicontrol(InputFig    ,              ...
    BtnInfo     , ...
    'Position'  ,[ FigWidth-3*BtnWidth-2*DefOffset DefOffset ...
    BtnWidth                    BtnHeight ...
    ]           , ...
    'String'     ,'Update'        , ...
    'Callback'   ,CBString    , ...
    'Tag'        ,'Update'          ...
    );

Data.UDHandle = UDHandle; % bcoe added
Data.OKHandle = OKHandle;
Data.CancelHandle = CancelHandle;
Data.EditHandles = EditHandle;
Data.QuestHandles = QuestHandle;
Data.LineInfo = NumLines;
Data.ButtonWidth = BtnWidth;
Data.ButtonHeight = BtnHeight;
Data.EditHeight = TxtHeight+4;
Data.Offset = DefOffset;
set(InputFig ,'Visible','on','UserData',Data);
% This drawnow is a hack to work around a bug
drawnow
set(findall(InputFig),'Units','normalized','HandleVisibility','callback');
set(InputFig,'Units','points')
try
    uiwait(InputFig);
catch
    delete(InputFig);
end

TempHide=get(0,'ShowHiddenHandles');
set(0,'ShowHiddenHandles','on');

% if any(get(0,'Children')==InputFig),
%     Answer={};
%     if strcmp(get(InputFig,'UserData'),'OK'),
%         Answer=cell(NumQuest,1);
%         for lp=1:NumQuest,
%             Answer(lp)=get(EditHandle(lp),{'String'});
%         end % for
%     end % if strcmp
%     delete(InputFig);
% else,
%     Answer={};
% end % if any

update=0;
if strcmp(get(InputFig,'UserData'),'Update'),
    update=1;
end;


if any(get(0,'Children')==InputFig),
    if strcmp(get(InputFig,'UserData'),'Cancel'),
        Answer={};
    else
        Answer=cell(NumQuest,1);
        for lp=1:NumQuest,
            Answer(lp)=get(EditHandle(lp),{'String'});
        end % for
    end % if strcmp
    delete(InputFig);
else
    Answer={};
end % if any

set(0,'ShowHiddenHandles',TempHide);


function LocalResizeFcn(FigHandle)
Data=get(FigHandle,'UserData');

%Data.ButtonHandles = [ OKHandles CancelHandle];
%Data.EditHandles = EditHandle;
%Data.QuestHandles = QuestHandle;
%Data.LineInfo = NumLines;
%Data.ButtonWidth = BtnWidth;
%Data.ButtonHeight = BtnHeight;
%Data.EditHeight = TxtHeight;

set(findall(FigHandle),'Units','points');

FigPos = get(FigHandle,'Position');
FigWidth = FigPos(3); FigHeight = FigPos(4);

OKPos = [ FigWidth-Data.ButtonWidth-Data.Offset Data.Offset ...
    Data.ButtonWidth                      Data.ButtonHeight ];
CancelPos =[Data.Offset Data.Offset Data.ButtonWidth  Data.ButtonHeight];
set(Data.OKHandle,'Position',OKPos);
set(Data.CancelHandle,'Position',CancelPos);

% Determine the height of all question fields
YPos = sum(OKPos(1,[2 4]))+Data.Offset;
QuestPos = get(Data.QuestHandles,{'Extent'});
QuestPos = cat(1,QuestPos{:});
QuestPos(:,1) = Data.Offset;
RemainingFigHeight = FigHeight - YPos - sum(QuestPos(:,4)) - ...
    Data.Offset - size(Data.LineInfo,1)*Data.Offset;

Num1Liners = length(find(Data.LineInfo(:,1)==1));

RemainingFigHeight = RemainingFigHeight - ...
    Num1Liners*Data.EditHeight;

Not1Liners = find(Data.LineInfo(:,1)~=1);

%Scale the 1 liner heights appropriately with remaining fig height
TotalLines = sum(Data.LineInfo(Not1Liners,1));

% Loop over each quest/text pair

for lp = 1:length(Data.QuestHandles),
    CurPos = get(Data.EditHandles(lp),'Position');
    NewPos = [Data.Offset YPos  CurPos(3) Data.EditHeight ];
    if Data.LineInfo(lp,1) ~= 1,
        NewPos(4) = RemainingFigHeight*Data.NumLines(lp,1)/TotalLines;
    end
    
    set(Data.EditHandles(lp),'Position',NewPos)
    YPos = sum(NewPos(1,[2 4]));
    QuestPos(lp,2) = YPos;QuestPos(lp,3) = NewPos(3);
    set(Data.QuestHandles(lp),'Position',QuestPos(lp,:));
    YPos = sum(QuestPos(lp,[2 4]))+Data.Offset;
end

if YPos>FigHeight - Data.Offset,
    FigHeight = YPos+Data.Offset;
    FigPos(4)=FigHeight;
    set(FigHandle,'Position',FigPos);
    drawnow
end
set(FigHandle,'ResizeFcn','inputdlg InputDlgResizeCB');

set(findall(FigHandle),'Units','normalized')

